import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/model/commodity_details_entity.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/util/rong_widget.dart';
import 'package:flutter_ncsjqz_getx/page_common/show_web_page.dart';
import 'package:flutter_ncsjqz_getx/util/pay_auth_share_util.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';

import 'farm_details_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/11/26 17:10
/// filePath： lib/page/farm/details/farm_details_view.dart
/// desc: 农场详情
/// <summary>
///
class FarmDetailsPage extends StatefulWidget {
  final int? shopId; // 商家id
  final int? farmId; // 农场id
  FarmDetailsPage({ this.shopId, this.farmId});
  @override
  _FarmDetailsPageState createState() => _FarmDetailsPageState();
}

class _FarmDetailsPageState extends State<FarmDetailsPage> {
  final logic = Get.put(FarmDetailsLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.getData(widget.farmId,widget.shopId);
  }

  @override
  Widget build(BuildContext context) {

    return GetBuilder<FarmDetailsLogic>(
      builder: (controller) {
        return Scaffold(
          appBar: appBarWidget(title: "详情页"),
          body: ListView(
            padding: EdgeInsets.all(0),
            children: [
              Stack(
                alignment: Alignment.topCenter,
                children: [
                  Container(
                    width: Get.width,
                    height: Get.height,
                    child: Column(
                      children: [
                        Container(
                          width: Get.width,
                          height: Get.width / (16/9),
                          child: logic.chewieController==null ? InkWidget.imgNet("${logic.commodityDetailData!.masterPic}",width: Get.width):
                          InkWell(
                            onTap: (){

                            },
                            child: Chewie(
                              controller: logic.chewieController!,
                            ),
                          ),
                          //  FijkView(
                          //   width: Get.width,
                          //   height: Get.width / (16/9),
                          //   player: logic.player!,
                          //   color: Colors.black,
                          // ),
                        ),
                      ],
                    ),
                  ),

                  Container(
                    width: Get.width - 30,
                    margin: EdgeInsets.only(top: (Get.width / (16/9) - 50),bottom: 15),
                    padding: EdgeInsets.all(15),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(8),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey.shade300,
                            blurRadius: 2,
                            spreadRadius: 2,
                          )
                        ]
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Expanded(child: InkWidget.title("${logic.commodityDetailData!.commodityName}"),),
                            InkWidget.text("  面积：${logic.commodityDetailData!.acreage}",color: Colors.grey),
                          ],
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        ),
                        SizedBox(height: 15,),
                        InkWidget.text("${logic.commodityDetailData!.brief}",maxLines: 99,color: Colors.grey),
                        SizedBox(height: 15,),
                        Container(
                          decoration: BoxDecoration(
                            border: Border(left: BorderSide(color: Colors.green,width: 2),),
                          ),
                          child: InkWidget.text(" 农场介绍",color: Colors.green),
                        ),
                        SizedBox(height: 15,),
                        Container(
                          child: InkWidget.text("--> 查看农场监控 <--",maxLines: 5,color: Colors.blue,onTap: (){
                            Get.to(()=>ShowWebPage(title: "${logic.commodityDetailData!.commodityName}",url: "${logic.commodityDetailData!.monitorLink}",quarterTurns: 1,));
                          }),
                        ),
                        SizedBox(height: 15,),
                        InkWidget.text("${logic.commodityDetailData!.content}",maxLines: 99,color: Colors.grey.shade800),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
          bottomNavigationBar: bottomMenu(),
        );
      },
    );
  }

  /// 规格选择
  bottomSkuMenu(){
    // 弹出时选择默认
    logic.setDefSku();
    Get.bottomSheet(
      Material(
        child: GetBuilder<FarmDetailsLogic>(
          builder: (_) {
            return WillPopScope(
              child: Container(
            width: Get.width,
            height: Get.height * 0.6,
            color: Colors.white,
            padding: EdgeInsets.all(15),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        InkWidget.imgNet('${logic.commodityDetailData!.masterPic!}',width: 80,height: 80,borderRadius: BorderRadius.circular(5)),
                        SizedBox(
                          width: 10,
                        ),
                        Container(
                          width: Get.width - 145,
                          height: 80,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              InkWidget.title("${logic.commodityDetailsDataSkuList.skuFullName}",maxLines: 1,fonSize: 14),
                              Builder(builder: (ctx,){
                                String name = logic.commodityDetailsDataSkuList.properties ?? '';
                                name = name.substring(0,name.lastIndexOf(";")!=-1 ?name.lastIndexOf(";"):name.length);
                                return InkWidget.text("规格：${name}",fonSize: 12);
                              }),
                              InkWidget.text("库存：${logic.commodityDetailsDataSkuList.actualStocks  ?? 0}",fonSize: 12),
                            ],
                          ),
                        ),
                      ],
                    ),

                    InkWidget.imgAsset("rem.png",width: 25,onTap: (){
                      Get.back();
                    }),
                  ],
                ),
                Divider(height: 20,color: Colors.grey.shade300,),
                // Container(
                //   decoration: BoxDecoration(
                //     border: Border(left: BorderSide(color: Colors.green,width: 2),),
                //   ),
                //   child: InkWidget.text(" 选择土地",color: Colors.green),
                // ),
                /// 多规格
                Expanded(
                  child: Container(
                    width: Get.width,
                    child: ListView(
                      shrinkWrap: true,
                      children: [
                        ...List.generate(logic.commodityDetailData!.props!.length, (index1){
                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              RongWidget.title("\n${logic.commodityDetailData!.props![index1].propName}\n",color: Colors.grey),
                              Wrap(
                                runSpacing: 5,
                                spacing: 5,
                                children: [
                                  ...List.generate(logic.commodityDetailData!.props![index1].propValues!.length, (index2){
                                    CommodityDetailsDataPropsPropValues shopDataContentProp =logic.commodityDetailData!.props![index1].propValues![index2];
                                    return InkWell(
                                      child:  Column(
                                        children: [
                                          if(shopDataContentProp.propUrl!="") Container(
                                            child: InkWidget.imgNet("${shopDataContentProp.propUrl}",width: Get.width / 6,height: Get.width / 6,borderRadius: BorderRadius.circular(5),),
                                            decoration: BoxDecoration(
                                              borderRadius: BorderRadius.circular(5),
                                              border: Border.all(color: !shopDataContentProp.select! ? Colors.transparent : Colors.green,width: 2),
                                            ),
                                          ),
                                          Container(
                                            child: InkWidget.text("${shopDataContentProp.propValue}",color: Colors.grey,fonSize: 14),
                                            decoration: shopDataContentProp.propUrl!="" ? BoxDecoration() : BoxDecoration(
                                              borderRadius: BorderRadius.circular(5),
                                              border: Border.all(color: !shopDataContentProp.select! ? Colors.transparent : Colors.green)
                                            ),
                                            padding: EdgeInsets.symmetric(horizontal: 10),
                                          ),
                                        ],
                                      ),
                                      onTap: (){
                                        // 除了选中的 全部设置成false
                                        logic.commodityDetailData!.props![index1].propValues!.forEach((element) {
                                          element.select=false;
                                        });
                                        // 设置选中的状态
                                        shopDataContentProp.select  = !shopDataContentProp.select!;
                                        logic.update();
                                        // 校验sku
                                        logic.checkShopSku();
                                      },
                                    );
                                  }),
                                ],
                              ),
                            ],
                          );
                        }),
                      ],
                    ),
                  ),
                ),
                /// 数量、总价
                Column(
                  children: [
                    Divider(height: 20,color: Colors.grey.shade300,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        InkWidget.text("购买数量"),
                        Row(
                          children: [
                            InkWell(
                              child: Icon(Icons.remove_circle_outline,size: 28,color: Colors.green,),
                              onTap: (){
                                if(logic.count>1) {
                                  logic.count--;
                                  logic.setCountPrice();
                                }
                              },
                            ),
                            Container(
                              width: 80,
                              margin: EdgeInsets.symmetric(horizontal: 5),
                              padding: EdgeInsets.symmetric(horizontal: 20,vertical: 5),
                              child: Center(
                                child: RongWidget.text(logic.count),
                              ),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(28),
                                  border: Border.all(color: Colors.green)
                              ),
                            ),
                            InkWell(
                              child: Icon(Icons.add_circle_outline,size: 28,color: Colors.green,),
                              onTap: (){
                                logic.count++;
                                logic.setCountPrice();
                              },
                            ),
                          ],
                        ),
                      ],
                    ),
                    Divider(height: 20,color: Colors.grey.shade300,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            InkWidget.title("总价：",color: Colors.green),
                            InkWidget.title("￥${logic.countPrice}",color: Colors.green),
                          ],
                        ),
                        SizedBox(
                          child: gradientButton("确定",circular: 35,onTap: (){
                            logic.submitShop();
                          }),
                          width: 100,
                          height: 35,
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
              onWillPop: () async => false,
            );
          },
        ),
      ),
      isScrollControlled: true,
      isDismissible: false,
    );
  }

  /// 底部菜单
  bottomMenu() {
    return Container(
      height: 60,
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(color: Colors.grey.shade200),
        ),
      ),
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          InkWidget.title("￥${logic.commodityDetailData!.price}",color: Colors.green,fonSize: 18),
          Row(
            children: [
              OutlineButton(
                onPressed: (){
                  bottomSheet();
                },
                child: InkWidget.text("分享好友",color: Colors.green),
                shape: StadiumBorder(),
                borderSide: BorderSide(color: Colors.green),
              ),
              SizedBox(width: 20,),
              gradientButton("购买",circular: 40,onTap: (){
                bottomSkuMenu();
              }),
            ],
          ),
        ],
      ),
    );
  }

  /// 分享弹出框
  bottomSheet(){
    Get.bottomSheet(
      GetBuilder<FarmDetailsLogic>(
        builder: (controller) {
          return baseScaffoldWidget(
            loading: logic.loading,
            child: InkWell(
              onTap: ()=> Get.back(),
              child: Container(
                height: Get.height,
                color: Colors.transparent,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          InkWidget.imgAsset("wx.png",width: 60,onTap: (){
                            logic.start();
                            PayUtil.wxShareImage(
                                pic: logic.commodityDetailData!.masterPic,
                                description: logic.commodityDetailData!.content,
                                title: logic.commodityDetailData!.commodityName,
                                webViewPath: 'http://fir.yr966.com/app.php/NTE1',
                                onCall: (v){
                                  logic.close();
                                }
                            );
                          }),
                          InkWidget.imgAsset("share-wx2png",width: 60,onTap: (){
                            logic.start();
                            PayUtil.wxShareImage(
                                pic: logic.commodityDetailData!.masterPic,
                                title: logic.commodityDetailData!.commodityName,
                                description: logic.commodityDetailData!.content,
                                webViewPath: logic.commodityDetailData!.commodityVideo,
                                scene: WeChatScene.TIMELINE,
                                onCall: (v){
                                  logic.close();
                                }
                            );
                          }),
                        ],
                      ),
                      color: Colors.white,
                      padding: EdgeInsets.symmetric(vertical: 20),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
      isScrollControlled: true,
    );
  }

  @override
  void dispose() {
    Get.delete<FarmDetailsLogic>();
    // if(logic.player != null) logic.player!.release();
    if(logic.chewieController!=null) logic.chewieController!.dispose();
    if(logic.videoPlayerController1!=null) logic.videoPlayerController1!.dispose();
    super.dispose();
  }
}